package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.commons.ResponseResult;
import cn.kgc.springcloud.dto.StudentDTO;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.util.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author mengshun
* @description 针对表【t_student】的数据库操作Service实现
* @createDate 2024-05-09 09:29:44
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{

    @Resource
    private CityClient cityClient;
    @Resource
    private GroupClient groupClient;
    @Resource
    private ClazzClient clazzClient;
    @Resource
    private TagClient tagClient;

    @Resource
    private StudentTagMapper studentTagMapper;

    @Override
    public ResponseResult studentList() {
        List<Student> students = baseMapper.selectList(null);

        // student   -> studentVO
        List<StudentVO> collect = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // 城市信息
            Integer cityid = student.getCityid();
            City oneCity = cityClient.getOneCity(cityid);
            studentVO.setCity(oneCity);

            // 班级信息
            Integer clazzid = student.getClazzid();
            Clazz oneClazz = clazzClient.getOneClazz(clazzid);
            studentVO.setClazz(oneClazz);

            // 小组信息
            Integer groupid = student.getGroupid();
            Group oneGroup = groupClient.getOneGroup(groupid);
            studentVO.setGroup(oneGroup);
            // 标签列表
            Integer studentId = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,studentId);

            List<Integer> tIds = studentTagMapper.selectList(lambda)
                    .stream()
                    .map(studentTag -> studentTag.getTid())
                    .collect(Collectors.toList());

            // 遍历标签id  获取标签详情   tag服务发起请求
            List<Tag> tags = tIds.stream().map(tid -> {
                Tag oneTag = tagClient.getOneTag(tid);
                return oneTag;
            }).collect(Collectors.toList());
            studentVO.setTags(tags);
            return studentVO;
        }).collect(Collectors.toList());


        return ResponseResult.success().data(collect);
    }

    @Override
    public ResponseResult addStudent(StudentDTO studentDTO) {

        //  studentDTO  -> student
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        Date bir = studentDTO.getBir();

        // 年龄
        int age = DateUtil.getAge(bir);
        student.setAge(age);
        // 星座
        String constellation = DateUtil.getConstellation(bir);
        student.setStarts(constellation);
        // 属相
        String year = DateUtil.getYear(bir);
        student.setAttr(year);

        // 添加学生信息到学生表    id
        baseMapper.insert(student);

        // 获取学生自增的主键
        Integer sId = student.getId();

        // 获取学生的标签信息
        List<Integer> tagIds = studentDTO.getTagIds();
        tagIds.forEach(tid->{
            StudentTag studentTag = new StudentTag();
            studentTag.setSid(sId);
            studentTag.setTid(tid);
            studentTagMapper.insert(studentTag);
        });

        // student  -> StudentVO
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student,studentVO);

        // 城市信息封装
        Integer cityid = student.getCityid();
        City oneCity = cityClient.getOneCity(cityid);
        studentVO.setCity(oneCity);


        // 班级信息的封装
        Integer clazzid = student.getClazzid();
        Clazz oneClazz = clazzClient.getOneClazz(clazzid);
        studentVO.setClazz(oneClazz);

        // 小组信息封装
        Integer groupid = student.getGroupid();
        Group oneGroup = groupClient.getOneGroup(groupid);
        studentVO.setGroup(oneGroup);

        // 标签信息的封装
        // 遍历标签id  获取标签详情   tag服务发起请求
        List<Tag> tags = tagIds.stream().map(tid -> {
            Tag oneTag = tagClient.getOneTag(tid);
            return oneTag;
        }).collect(Collectors.toList());
        studentVO.setTags(tags);

        return ResponseResult.success().data(studentVO);
    }
}




